import { Card, message, Row, Col, Space } from 'antd';
import { history } from 'umi';
import ProForm, { ProFormText, ProFormSelect } from '@ant-design/pro-form';
import { useRequest } from 'umi';
import type { FC } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import { fakeSubmitForm, queryDetail, update } from './service';
import { hospitalList } from '@/services/ant-design-pro/api';

const BasicForm: FC<Record<string, any>> = () => {
  const { run } = useRequest(fakeSubmitForm, {
    manual: true,
    onSuccess: () => {
      message.success('提交成功');
    },
  });

  // const { data, loading } = useRequest(() => {
  //   if (history.location.query.id) {
  //     return queryDetail(history.location.query);
  //   }
  // });

  const onFinish = async (values: Record<string, any>) => {
    console.log('values11', values);
    if (history.location.query.id) {
      values.id = history.location.query.id;
      try {
        const response = await update(values);
        if (response.code == 200) {
          message.success('修改成功');
        } else {
          message.error('修改失败!');
        }
      } catch (error) {
        console.log(error);
      }
    } else {
      run(values);
    }
  };

  const formItemLayout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 14 },
  };

  return (
    <PageContainer content="">
      <Card bordered={false}>
        <ProForm
          hideRequiredMark
          style={{ margin: 'auto', marginTop: 8 }}
          name="basic"
          layout="horizontal"
          // initialValues={data}
          onFinish={onFinish}
          {...formItemLayout}
          request={async () => {
            if (history.location.query.id) {
              return queryDetail(history.location.query).then(({ data }) => {
                return data;
              });
            } else {
              return {};
            }
          }}
          submitter={{
            render: (props, doms) => {
              return (
                <Row>
                  <Col span={14} offset={4}>
                    <Space>{doms}</Space>
                  </Col>
                </Row>
              );
            },
          }}
        >
          <ProFormSelect
            width="md"
            name="hospitalId"
            label="医院"
            showSearch
            // fieldProps={{
            //   labelInValue: true,
            //   labelCol: { span: 8 },
            //   wrapperCol: { span: 14 },
            // }}
            request={async ({ keyWords }) => {
              let query = {
                likeQuery: {
                  name: keyWords,
                },
                pageNum: 1,
                pageSize: 20,
              };
              return hospitalList(query).then(({ data }) => {
                return data.list.map((item) => {
                  return {
                    label: item.name,
                    value: item.id,
                  };
                });
              });
            }}
          />
          <ProFormText
            width="md"
            label="公告文章"
            name="articleId"
            rules={[
              {
                required: true,
                message: '请输入公告文章',
              },
            ]}
            placeholder="请输入公告文章"
          />
        </ProForm>
      </Card>
    </PageContainer>
  );
};

export default BasicForm;
